package org.yzh.rbac.controller;

import io.swagger.annotations.Api;
import org.sample.commons.lang.StrUtils;
import org.sample.component.mybatis.Page;
import org.sample.model.APIResult;
import org.sample.model.PageInfo;
import org.sample.model.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yzh.rbac.model.entity.RoleDO;
import org.yzh.rbac.service.RoleService;

import java.time.LocalDateTime;

@Api(description = "角色API")
@RequestMapping("role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping
    public Pagination<RoleDO> list(RoleDO query, PageInfo pageInfo) {
        Pagination result = Page.start(() -> roleService.find(query), pageInfo);
        return result;
    }

    @GetMapping("detail")
    public RoleDO list(@RequestParam Integer id) {
        RoleDO result = roleService.get(id);
        return result;
    }

    @PostMapping
    public APIResult<Boolean> add(RoleDO entity) {
        entity.setCreateTime(LocalDateTime.now());
        entity.setPid(1);
        int row = roleService.add(entity);
        return new APIResult(row > 0);
    }

    @PutMapping
    public APIResult<Boolean> update(RoleDO entity) {
        int row = roleService.update(entity);
        return new APIResult(row > 0);
    }

    @DeleteMapping
    public APIResult<Boolean> delete(@RequestParam String id) {
        int[] ids = StrUtils.toInts(id, ",");
        for (int i : ids)
            roleService.delete(i);
        return APIResult.SUCCESS;
    }
}